package cn.karent.common;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;

import java.util.List;
import java.util.function.Function;

/**
 * @author wanshengdao
 * @date 2025/4/15
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class Pageable<T> {

    private String retCode = "00";

    private String retMsg = "Success";

    private Integer total;

    private Integer totalPages;

    private Integer num;

    private Integer page;

    private List<T> data;

    public static <S, T> Pageable<T> transfer(Pageable<S> data, Function<S, T> function) {
        List<S> sourceData = data.getData();
        List<T> collect = sourceData.stream().map(function).toList();
        Pageable<T> pageable = new Pageable<>();
        pageable.setData(collect);
        pageable.setNum(data.getNum());
        pageable.setTotalPages(data.getTotalPages());
        pageable.setTotal(data.getTotal());
        pageable.setPage(data.getPage());
        return pageable;
    }

}
